﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmManagePlatform.Forms
{
    public partial class UserInfoForm : RelationsLMP.Control.Form.MetroDemoForms
    {
        public Model.DB.tb_User UserInfo { get; set; }

        public UserInfoForm()
        {
            InitializeComponent();

            InitControls();

            UserInfo = new Model.DB.tb_User();
        }

        public void ModifyInfo(Model.DB.tb_User temp)
        {
            UserInfo.userId = temp.userId;
            UserInfo.userName = temp.userName;

            txtName.Text = temp.userName;
            txtName.Enabled = false;

            cmbRole.SelectedValue = temp.roleId;

            txtPassWord.Text = temp.passWord;
            txtCofirmPassWord.Text = temp.passWord;       
        }

        private void InitControls()
        {
            InitComBoxRoleInfo();

            okReturnToolbar21.Event_OKClick += OkReturnToolbar21_Event_OKClick;
            okReturnToolbar21.Event_ReturnClick += OkReturnToolbar21_Event_ReturnClick;
        }

        private void OkReturnToolbar21_Event_OKClick(object sender, EventArgs e)
        {

            if (string.IsNullOrWhiteSpace(txtName.Text))
            {
                txtName.Focus();
                MessageBox.Show(this, "【账户】不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrWhiteSpace(txtPassWord.Text))
            {
                txtPassWord.Focus();
                MessageBox.Show(this, "【密码】不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (txtPassWord.Text != txtCofirmPassWord.Text)
            {
                txtCofirmPassWord.Focus();
                MessageBox.Show(this, "【确认密码】错误，请重新输入！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (UserInfo.userName != txtName.Text &&  new DataAccess.tb_User().CheckUserNameIsExist(txtName.Text))
            {
                txtName.Focus();
                MessageBox.Show(this, "【账户】已存在！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            UserInfo = new Model.DB.tb_User();
            UserInfo.userName = txtName.Text;
            UserInfo.passWord = txtPassWord.Text;
            UserInfo.roleId = int.Parse(cmbRole.SelectedValue.ToString());

            this.DialogResult = DialogResult.OK;
        }

        private void OkReturnToolbar21_Event_ReturnClick(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void InitComBoxRoleInfo()
        {
            Static.Methods.InitComBox(ref cmbRole, Model.SystemEnum.ComBoxType.Role);
        }
    }
}
